package com.yun.blog.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.yun.blog.bean.Album;
import com.yun.blog.bean.PicSaveBean;
import com.yun.blog.bean.Picture;
import com.yun.blog.bean.Result;
import com.yun.blog.service.AlbumService;
import com.yun.blog.service.PictureService;
import com.yun.blog.utils.FileUtils;
import com.yun.blog.utils.QianiuUploadUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;


/**
 * title        : PictureController
 * projectName  : dantouBlog-back
 * description  : 相册图片控制类
 * author       : simple
 * date         : 2022/12/2下午3:40
 */

@Slf4j
@RestController
@RequestMapping("/picture")
public class PictureController {

    @Resource
    PictureService pictureService;
    @Resource
    AlbumService albumService;

    @GetMapping("/getPicturesByAlbumID")
    public Result getPicturesByAlbumID(Integer albumId) {

        LambdaQueryWrapper<Picture> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(Picture::getAlbumId, albumId);

        List<Picture> list = pictureService.list(queryWrapper);
        return Result.success().add("pictureList", list);
    }

    @Transactional
    @PostMapping("/savePictures")
    public Result savePictures(@RequestBody PicSaveBean picSaveBean) {
        try {
            for (HashMap<String, String> pictureMap : picSaveBean.getPicList()) {
                Picture picture = new Picture();
                String name = pictureMap.get("name");
                if (name.length() > 20) {
                    name = name.substring(name.length() - 20);
                }
                picture.setPictureName(name);
                picture.setPictureUrl(pictureMap.get("url"));
                picture.setAlbumId(picSaveBean.getAlbumId());
                pictureService.save(picture);
            }
            return Result.success("保存成功！");
        } catch (Exception e) {
            log.error(e.getMessage());
            return Result.fail("保存失败！错误信息为：" + e.getMessage());
        }

    }

    @GetMapping("/deletePicture")
    public Result deletePicture(Integer pictureId) {
        try {
            pictureService.removeById(pictureId);
        } catch (Exception e) {
            log.error("删除失败：错误信息为：{}", e.getMessage());
            return Result.fail(e.getMessage());
        }
        return Result.success("删除成功！");
    }

    @GetMapping("/setAsCover")
    public Result setAsCover(Integer albumId, String pictureUrl) {
        log.info("albumId:{}",albumId);
        log.info("pictureUrl:{}",pictureUrl);
        try {
            Album album = albumService.getByAlbumId(albumId);
            album.setAlbumCover(pictureUrl);
            albumService.saveOrUpdate(album);
        } catch (Exception e) {
            log.error("修改失败：错误信息为：{}", e.getMessage());
            return Result.fail(e.getMessage());
        }
        return Result.success("修改成功！");
    }

    @PostMapping("/addPicture")
    public Result addPicture(MultipartFile picture, Integer albumId) {
        Result fail = Result.fail();
        if (picture == null || picture.isEmpty()) {
            fail.setMsg("图片不能为空！");
            return fail;
        }
        String filename = picture.getOriginalFilename();
        boolean allowPic = FileUtils.isAllowPic(filename);
        if (!allowPic) {
            fail.setMsg("图片类型不正确！");
            return fail;
        }

        try {
            // String imgPath = saveImg(picture, "picture/" + albumId);
            String imgPath = QianiuUploadUtils.upload(picture, "picture/" + albumId);
            Result success = Result.success("图片保存成功！");
            HashMap<String, String> pictureMap = new HashMap<>();
            pictureMap.put("name", filename);
            pictureMap.put("url", imgPath);
            success.add("pictureMap", pictureMap);
            return success;
        } catch (Exception e) {
            log.error("图片保存失败！，错误信息为：{}", e.getMessage());
            fail.setMsg("图片保存失败！，错误信息为：" + e.getMessage());
            return fail;
        }
    }
}
